import dayjs from 'dayjs';
import { api } from '../../../../config/api';
import { request } from '../../../../utils/request';

const emptyCouponImg = `https://core.jucheap.com/shop/attachment/images/ordersure-coupon-newempty.png`;

Component({
	properties: {
		storeId: String,
		promotionGoodsList: {
			type: Array,
			value: [],
		},
		orderSureCouponList: {
			type: Array,
			value: [],
		},
		couponsShow: {
			type: Boolean,
			value: false,
			observer(couponsShow) {
				if (couponsShow) {
					const { promotionGoodsList, orderSureCouponList, storeId } =
						this.data;
					const products =
						promotionGoodsList &&
						promotionGoodsList.map((goods) => {
							this.storeId = goods.storeId;
							return {
								skuId: goods.skuId,
								spuId: goods.spuId,
								storeId: goods.storeId,
								selected: true,
								quantity: goods.quantity,
								price: goods.price
							};
						});
					const selectedCoupons =
						orderSureCouponList &&
						orderSureCouponList.map((ele) => {
							return {
								promotionId: ele.promotionId,
								storeId: ele.storeId,
								couponId: ele.couponId,
							};
						});
					this.setData({
						products,
					});
					this.coupons({
						products,
						selectedCoupons,
						storeId,
					}).then((res) => {
						this.initData(res);
					});
				}
			},
		},
	},
	data: {
		emptyCouponImg,
		goodsList: [],
		selectedList: [],
		couponsList: [],
		orderSureCouponList: [],
		promotionGoodsList: [],
	},
	methods: {
		initData(data = {}) {
			const { couponResultList = [], reduce = 0 } = data;
			const selectedList = [];
			let selectedNum = 0;
			const couponsList =
				couponResultList &&
				couponResultList.map((coupon) => {
					const { status, couponVO } = coupon;
					const {
						couponId,
						condition = '',
						endTime = 0,
						name = '',
						startTime = 0,
						value,
						type,
						unavailableReason
					} = couponVO;
					//默认选中的优惠券
					if (status === 1) {
						selectedNum++;
						selectedList.push({
							key: couponId,
							couponId,
							promotionId: '',
							storeId: this.storeId,
						});
					}
					let frontStatus = 'default';
					if (status === -1) {
						frontStatus = 'useless';
					} else if (status === 2) {
						frontStatus = 'unavailable';
					}
					return {
						key: couponId,
						couponId: couponId,
						title: name,
						isSelected: false,
						timeLimit: `${dayjs(+startTime).format('YYYY-MM-DD')}-${dayjs(
							+endTime,
						).format('YYYY-MM-DD')}`,
						value: value,
						status: frontStatus,
						desc: condition,
						unavailableReason: unavailableReason,
						type,
						tag: '',
					};
				});
			this.setData({
				selectedList,
				couponsList,
				reduce,
				selectedNum,
			});
		},
		selectCoupon(e) {
			const { key } = e.currentTarget.dataset;
			const { couponsList, selectedList } = this.data;
			couponsList.forEach((coupon) => {
				if (coupon.key === key) {
					coupon.isSelected = !coupon.isSelected;
				}
			});

			const couponSelected = couponsList.filter(
				(coupon) => coupon.isSelected === true,
			);

			this.setData({
				selectedList: [...selectedList, ...couponSelected],
				couponsList: [...couponsList],
			});
			const mySelectedCoupons = {
				selectedList: [...selectedList, ...couponSelected]
			};
			this.triggerEvent('sure', mySelectedCoupons);
		},
		hide() {
			this.setData({
				couponsShow: false,
			});
		},
		coupons(coupon = {}) {
			console.log('选择店铺优惠券参数：', coupon);
			return request.post(api.coupon.store, coupon);
			// return new Promise((resolve, reject) => {
			//   if (coupon?.selectedCoupons) {
			//     resolve({
			//       couponResultList: couponsData.couponResultList,
			//       reduce: couponsData.reduce,
			//     });
			//   }
			//   return reject({
			//     couponResultList: [],
			//     reduce: undefined,
			//   });
			// });
		},
	},
});
